package com.szj360.spider.pipeline;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.szj360.entity.JpressContent;
import com.szj360.service.JpressContentService;
import com.szj360.utils.ImageBase64Utils;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

@Service("JobInfoDaoPipeline")
public class JobInfoDaoPipeline implements Pipeline {

	@Autowired
	private JpressContentService jpressContentService;

	public JobInfoDaoPipeline() {
		super();
	}

	@Override
	public void process(ResultItems resultItems, Task task) {
		// Set<String> keys = resultItems.getAll().keySet();
		// for (Iterator<String> it = keys.iterator(); it.hasNext();) {
		// String key = it.next();
		// Logger.getLogger().log(Level.INFO, key + ":" + resultItems.get(key));
		// }

		// 如果是新闻
		if ("detailpage".equals(resultItems.get("type"))) {
			// 查询是否重复
			JpressContent jc = new JpressContent();
			jc.setRemarks(String.valueOf(resultItems.get("src")));

			List l = this.jpressContentService.findByRemarks(jc.getRemarks());
			if (l != null && l.size() > 0) {
				return;
			}
			if (resultItems.get("title") == null)
				return;
			jc.setTitle(String.valueOf(resultItems.get("title")));
			jc.setText(String.valueOf(resultItems.get("content")));
			jc.setCreated(new Date());
			jc.setModule("article");
			jc.setStatus("normal");
			jc.setUserId(1);
			String slug = jc.getTitle();
			if (slug != null) {
				if (StringUtils.isNumeric(slug)) {
					slug = "c" + slug; // slug不能为全是数字,随便添加一个字母，c代表content好了
				} else {
					slug = slug.replaceAll("(\\s+)|(\\.+)|(。+)|(…+)|[\\$,，？\\-?、；;:!]", "_");
					slug = slug.replaceAll("(?!_)\\pP|\\pS", "");
				}
				jc.setSlug(slug);
			}

			this.jpressContentService.saveContent(jc);
		} else if("img".equals(resultItems.get("type"))){
			String imgStr = resultItems.get("img");
			String link = resultItems.get("link");
//			String imgFilePath = "d:\\img\\" + link.split("/")[link.split("/").length-1];// 新生成的图片
			String imgFilePath = link.split("/")[link.split("/").length-1];// 新生成的图片

//			上传至七牛并得到地址
			String imgLink = ImageBase64Utils.generateImage(imgFilePath, imgStr);
//			更新地址
//			System.out.println(imgLink);
			Logger.getLogger(getClass()).info(imgLink);
			
			String ref = String.valueOf(resultItems.get("contentUrl"));
			List<JpressContent> jc = this.jpressContentService.findByRemarks(ref);
			if(jc!=null&&jc.size()>0){
				for (JpressContent jpressContent : jc) {
					String content = jpressContent.getText();
					content = content.replaceAll(link, imgLink);
					jpressContent.setText(content);
					this.jpressContentService.saveContent(jpressContent);
				}
			}else{
				Logger.getLogger(this.getClass()).error("content ref not found for "+ ref);
				
			}

		}

	}

	// @Resource
	// private JobInfoDAO jobInfoDAO;
}